package com.example.kyyl.http;

import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;

class RequestUtil {
    /* access modifiers changed from: private */
    public CallBackUtil mCallBack;
    private File mFile;
    private String mFileType;
    private Map<String, String> mHeaderMap;
    private String mJsonStr;
    private String mMetyodType;
    private OkHttpClient mOkHttpClient;
    private Request mOkHttpRequest;
    private Map<String, String> mParamsMap;
    private Request.Builder mRequestBuilder;
    private String mUrl;
    private String mfileKey;
    private List<File> mfileList;
    private Map<String, File> mfileMap;

    RequestUtil(String str, String str2, Map<String, String> map, Map<String, String> map2, CallBackUtil callBackUtil) {
        this(str, str2, (String) null, (File) null, (List<File>) null, (String) null, (Map<String, File>) null, (String) null, map, map2, callBackUtil);
    }

    private RequestUtil(String str, String str2, String str3, File file, List<File> list, String str4, Map<String, File> map, String str5, Map<String, String> map2, Map<String, String> map3, CallBackUtil callBackUtil) {
        this.mMetyodType = str;
        this.mUrl = str2;
        this.mJsonStr = str3;
        this.mFile = file;
        this.mfileList = list;
        this.mfileKey = str4;
        this.mfileMap = map;
        this.mFileType = str5;
        this.mParamsMap = map2;
        this.mHeaderMap = map3;
        this.mCallBack = callBackUtil;
        getInstance();
    }

    private void getInstance() {
        this.mOkHttpClient = new OkHttpClient();
        this.mRequestBuilder = new Request.Builder();
        if (this.mFile == null && this.mfileList == null && this.mfileMap == null) {
            String str = this.mMetyodType;
            char c = 65535;
            int hashCode = str.hashCode();
            if (hashCode != 70454) {
                if (hashCode != 79599) {
                    if (hashCode != 2461856) {
                        if (hashCode == 2012838315 && str.equals(OkhttpUtil.METHOD_DELETE)) {
                            c = 3;
                        }
                    } else if (str.equals(OkhttpUtil.METHOD_POST)) {
                        c = 1;
                    }
                } else if (str.equals(OkhttpUtil.METHOD_PUT)) {
                    c = 2;
                }
            } else if (str.equals(OkhttpUtil.METHOD_GET)) {
                c = 0;
            }
            switch (c) {
                case 0:
                    setGetParams();
                    break;
                case 1:
                    this.mRequestBuilder.post(getRequestBody());
                    break;
                case 2:
                    this.mRequestBuilder.put(getRequestBody());
                    break;
                case 3:
                    this.mRequestBuilder.delete(getRequestBody());
                    break;
            }
        } else {
            setFile();
        }
        this.mRequestBuilder.url(this.mUrl);
        if (this.mHeaderMap != null) {
            setHeader();
        }
        this.mOkHttpRequest = this.mRequestBuilder.build();
    }

    private void setGetParams() {
        if (this.mParamsMap != null) {
            this.mUrl += "?";
            for (String next : this.mParamsMap.keySet()) {
                this.mUrl += next + "=" + this.mParamsMap.get(next) + "&";
            }
            this.mUrl = this.mUrl.substring(0, this.mUrl.length() - 1);
        }
    }

    private RequestBody getRequestBody() {
        if (!TextUtils.isEmpty(this.mJsonStr)) {
            return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), this.mJsonStr);
        }
        FormBody.Builder builder = new FormBody.Builder();
        if (this.mParamsMap != null) {
            for (String next : this.mParamsMap.keySet()) {
                builder.add(next, this.mParamsMap.get(next));
            }
        }
        return builder.build();
    }

    private void setFile() {
        if (this.mFile != null) {
            if (this.mParamsMap == null) {
                setPostFile();
            } else {
                setPostParameAndFile();
            }
        } else if (this.mfileList != null) {
            setPostParameAndListFile();
        } else if (this.mfileMap != null) {
            setPostParameAndMapFile();
        }
    }

    private void setHeader() {
        if (this.mHeaderMap != null) {
            for (String next : this.mHeaderMap.keySet()) {
                this.mRequestBuilder.addHeader(next, this.mHeaderMap.get(next));
            }
        }
    }

    private void setPostFile() {
        if (this.mFile != null && this.mFile.exists()) {
            this.mRequestBuilder.post(new ProgressRequestBody(RequestBody.create(MediaType.parse(this.mFileType), this.mFile), this.mCallBack));
        }
    }

    private void setPostParameAndFile() {
        if (this.mParamsMap != null && this.mFile != null) {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.setType(MultipartBody.FORM);
            for (String next : this.mParamsMap.keySet()) {
                builder.addFormDataPart(next, this.mParamsMap.get(next));
            }
            builder.addFormDataPart(this.mfileKey, this.mFile.getName(), RequestBody.create(MediaType.parse(this.mFileType), this.mFile));
            this.mRequestBuilder.post(new ProgressRequestBody(builder.build(), this.mCallBack));
        }
    }

    private void setPostParameAndListFile() {
        if (this.mfileList != null) {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.setType(MultipartBody.FORM);
            if (this.mParamsMap != null) {
                for (String next : this.mParamsMap.keySet()) {
                    builder.addFormDataPart(next, this.mParamsMap.get(next));
                }
            }
            for (File next2 : this.mfileList) {
                builder.addFormDataPart(this.mfileKey, next2.getName(), RequestBody.create(MediaType.parse(this.mFileType), next2));
            }
            this.mRequestBuilder.post(builder.build());
        }
    }

    private void setPostParameAndMapFile() {
        if (this.mfileMap != null) {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.setType(MultipartBody.FORM);
            if (this.mParamsMap != null) {
                for (String next : this.mParamsMap.keySet()) {
                    builder.addFormDataPart(next, this.mParamsMap.get(next));
                }
            }
            for (String next2 : this.mfileMap.keySet()) {
                builder.addFormDataPart(next2, this.mfileMap.get(next2).getName(), RequestBody.create(MediaType.parse(this.mFileType), this.mfileMap.get(next2)));
            }
            this.mRequestBuilder.post(builder.build());
        }
    }

    RequestUtil(String str, String str2, String str3, Map<String, String> map, CallBackUtil callBackUtil) {
        this(str, str2, str3, (File) null, (List<File>) null, (String) null, (Map<String, File>) null, (String) null, (Map<String, String>) null, map, callBackUtil);
    }

    RequestUtil(String str, String str2, Map<String, String> map, File file, String str3, String str4, Map<String, String> map2, CallBackUtil callBackUtil) {
        this(str, str2, (String) null, file, (List<File>) null, str3, (Map<String, File>) null, str4, map, map2, callBackUtil);
    }

    RequestUtil(String str, String str2, Map<String, String> map, List<File> list, String str3, String str4, Map<String, String> map2, CallBackUtil callBackUtil) {
        this(str, str2, (String) null, (File) null, list, str3, (Map<String, File>) null, str4, map, map2, callBackUtil);
    }

    RequestUtil(String str, String str2, Map<String, String> map, Map<String, File> map2, String str3, Map<String, String> map3, CallBackUtil callBackUtil) {
        this(str, str2, (String) null, (File) null, (List<File>) null, (String) null, map2, str3, map, map3, callBackUtil);
    }

    /* access modifiers changed from: package-private */
    public void execute() {
        this.mOkHttpClient.newCall(this.mOkHttpRequest).enqueue(new Callback() {
            public void onFailure(Call call, IOException iOException) {
                if (RequestUtil.this.mCallBack != null) {
                    RequestUtil.this.mCallBack.onError(call, iOException);
                }
            }

            public void onResponse(Call call, Response response) throws IOException {
                if (RequestUtil.this.mCallBack != null) {
                    RequestUtil.this.mCallBack.onSeccess(call, response);
                }
            }
        });
    }

    private static class ProgressRequestBody extends RequestBody {
        private final RequestBody requestBody;
        /* access modifiers changed from: private */
        public CallBackUtil callBack;
        private BufferedSink bufferedSink;

        ProgressRequestBody(RequestBody requestBody2, CallBackUtil callBackUtil) {
            this.requestBody = requestBody2;
            this.callBack = callBackUtil;
        }

        public MediaType contentType() {
            return this.requestBody.contentType();
        }

        public long contentLength() throws IOException {
            return this.requestBody.contentLength();
        }

        public void writeTo(BufferedSink bufferedSink2) throws IOException {
            if (this.bufferedSink == null) {
                this.bufferedSink = Okio.buffer(sink(bufferedSink2));
            }
            this.requestBody.writeTo(this.bufferedSink);
            this.bufferedSink.flush();
        }

        private Sink sink(BufferedSink bufferedSink2) {
            return new ForwardingSink(bufferedSink2) {
                long bytesWritten = 0;
                long contentLength = 0;

                public void write(Buffer buffer, long j) throws IOException {
                    super.write(buffer, j);
                    if (this.contentLength == 0) {
                        this.contentLength = ProgressRequestBody.this.contentLength();
                    }
                    this.bytesWritten += j;
                    final float f = (((float) this.bytesWritten) * 1.0f) / ((float) this.contentLength);
                    CallBackUtil.mMainHandler.post(new Runnable() {
                        public void run() {
                            ProgressRequestBody.this.callBack.onProgress(f, contentLength);
                        }
                    });
                }
            };
        }
    }
}
